{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "200"
      ]
     },
     "execution_count": 1,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 导入需要的模块\n",
    "import requests\n",
    "import json\n",
    "\n",
    "# 输入密钥\n",
    "KEY = 'd4147415eb404d1fb95b61fdb32649ac'\n",
    "\n",
    "# 目标URL\n",
    "BASE_URL = 'https://apidyx.cognitiveservices.azure.com/face/v1.0/detect'\n",
    "\n",
    "# 请求头\n",
    "HEADERS = {\n",
    "    'Content-Type': 'application/json',\n",
    "    'Ocp-Apim-Subscription-Key': '{}'.format(KEY), \n",
    "}\n",
    "\n",
    "# 人脸相片地址\n",
    "img_url = 'https://www.sonystyle.com.cn/content/dam/sonystyle/products/ilc/e-body/ilce-7c/feature/ilce_7c_01_200915.jpg'\n",
    "data = {\n",
    "    'url': '{}'.format(img_url),\n",
    "}\n",
    "\n",
    "# 选择需要的人脸识别功能（根据API文档）\n",
    "payload = {\n",
    "    'returnFaceId': 'true',\n",
    "    'returnFaceLandmarks': 'flase',\n",
    "    'returnFaceAttributes': '{}'.format('age,gender,glasses,emotion'), \n",
    "}\n",
    "\n",
    "# 发送请求\n",
    "r = requests.post(BASE_URL,data=json.dumps(data),params = payload,headers=HEADERS)\n",
    "r.status_code"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "b'[{\"faceId\":\"bfadb94d-797a-4fa8-b332-c1e681175932\",\"faceRectangle\":{\"top\":106,\"left\":479,\"width\":158,\"height\":158},\"faceAttributes\":{\"gender\":\"female\",\"age\":21.0,\"glasses\":\"NoGlasses\",\"emotion\":{\"anger\":0.0,\"contempt\":0.0,\"disgust\":0.0,\"fear\":0.0,\"happiness\":0.006,\"neutral\":0.968,\"sadness\":0.003,\"surprise\":0.022}}}]'"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r.content"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[{'faceId': 'bfadb94d-797a-4fa8-b332-c1e681175932',\n",
       "  'faceRectangle': {'top': 106, 'left': 479, 'width': 158, 'height': 158},\n",
       "  'faceAttributes': {'gender': 'female',\n",
       "   'age': 21.0,\n",
       "   'glasses': 'NoGlasses',\n",
       "   'emotion': {'anger': 0.0,\n",
       "    'contempt': 0.0,\n",
       "    'disgust': 0.0,\n",
       "    'fear': 0.0,\n",
       "    'happiness': 0.006,\n",
       "    'neutral': 0.968,\n",
       "    'sadness': 0.003,\n",
       "    'surprise': 0.022}}}]"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "results = r.json()\n",
    "results"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>faceId</th>\n",
       "      <th>faceRectangle.top</th>\n",
       "      <th>faceRectangle.left</th>\n",
       "      <th>faceRectangle.width</th>\n",
       "      <th>faceRectangle.height</th>\n",
       "      <th>faceAttributes.gender</th>\n",
       "      <th>faceAttributes.age</th>\n",
       "      <th>faceAttributes.glasses</th>\n",
       "      <th>faceAttributes.emotion.anger</th>\n",
       "      <th>faceAttributes.emotion.contempt</th>\n",
       "      <th>faceAttributes.emotion.disgust</th>\n",
       "      <th>faceAttributes.emotion.fear</th>\n",
       "      <th>faceAttributes.emotion.happiness</th>\n",
       "      <th>faceAttributes.emotion.neutral</th>\n",
       "      <th>faceAttributes.emotion.sadness</th>\n",
       "      <th>faceAttributes.emotion.surprise</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>bfadb94d-797a-4fa8-b332-c1e681175932</td>\n",
       "      <td>106</td>\n",
       "      <td>479</td>\n",
       "      <td>158</td>\n",
       "      <td>158</td>\n",
       "      <td>female</td>\n",
       "      <td>21.0</td>\n",
       "      <td>NoGlasses</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.006</td>\n",
       "      <td>0.968</td>\n",
       "      <td>0.003</td>\n",
       "      <td>0.022</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                                 faceId  faceRectangle.top  \\\n",
       "0  bfadb94d-797a-4fa8-b332-c1e681175932                106   \n",
       "\n",
       "   faceRectangle.left  faceRectangle.width  faceRectangle.height  \\\n",
       "0                 479                  158                   158   \n",
       "\n",
       "  faceAttributes.gender  faceAttributes.age faceAttributes.glasses  \\\n",
       "0                female                21.0              NoGlasses   \n",
       "\n",
       "   faceAttributes.emotion.anger  faceAttributes.emotion.contempt  \\\n",
       "0                           0.0                              0.0   \n",
       "\n",
       "   faceAttributes.emotion.disgust  faceAttributes.emotion.fear  \\\n",
       "0                             0.0                          0.0   \n",
       "\n",
       "   faceAttributes.emotion.happiness  faceAttributes.emotion.neutral  \\\n",
       "0                             0.006                           0.968   \n",
       "\n",
       "   faceAttributes.emotion.sadness  faceAttributes.emotion.surprise  \n",
       "0                           0.003                            0.022  "
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import pandas as pd\n",
    "df = pd.json_normalize(results)\n",
    "df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 创建face list 并且人脸相似度"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "b''"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import requests\n",
    "# 1.创建人脸列表\n",
    "faceListId = \"dengyingxin02\" # 设置人脸列表ID\n",
    "create_facelists_url =\"https://apidyx.cognitiveservices.azure.com/face/v1.0/facelists/{}\" # URL（见API文档）\n",
    "subscription_key =\"d4147415eb404d1fb95b61fdb32649ac\" # 密钥\n",
    "# 请求头\n",
    "headers = {\n",
    "    'Content-Type': 'application/json',\n",
    "    'Ocp-Apim-Subscription-Key': subscription_key,\n",
    "}\n",
    "# 相关信息及识别模型的选用\n",
    "data = {\n",
    "  \"name\": \"sample_list\",\n",
    "  \"userData\": \"相册\",\n",
    "  \"recognitionModel\": \"recognition_03\"\n",
    "}\n",
    "# 发送请求\n",
    "r_create = requests.put(create_facelists_url.format(faceListId), headers=headers, json=data)\n",
    "r_create.content"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "200"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "add_face_url =\"https://apidyx.cognitiveservices.azure.com/face/v1.0/facelists/{}/persistedfaces\"\n",
    "assert subscription_key\n",
    "headers = {\n",
    "    'Content-Type': 'application/json',\n",
    "    'Ocp-Apim-Subscription-Key': subscription_key,\n",
    "}\n",
    "# 人脸相片地址\n",
    "img_url =\"https://www.sonystyle.com.cn/content/dam/sonystyle/products/ilc/e-body/ilce-7c/feature/ilce_7c_01_200915.jpg\"\n",
    "# 人脸数据\n",
    "params_add_face={\n",
    "    \"userData\":\"lady\"\n",
    "}\n",
    "# 发送请求\n",
    "r_add_face = requests.post(add_face_url.format(faceListId),headers=headers,params=params_add_face,json={\"url\":img_url})\n",
    "r_add_face.status_code"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'persistedFaceId': 'f7f05c9c-77e5-4227-91a5-4caa18044b86'}"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r_add_face.json() # 返回persistedFaceId"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 封装成函数方便添加图片\n",
    "def AddFace(img_url=str,userData=str):\n",
    "    add_face_url =\"https://apidyx.cognitiveservices.azure.com/face/v1.0/facelists/{}/persistedFaces\"\n",
    "    assert subscription_key\n",
    "    headers = {\n",
    "        # Request headers\n",
    "        'Content-Type': 'application/json',\n",
    "        'Ocp-Apim-Subscription-Key': subscription_key,\n",
    "    }\n",
    "    img_url = img_url\n",
    "\n",
    "    params_add_face={\n",
    "        \"userData\":userData\n",
    "    }\n",
    "    r_add_face = requests.post(add_face_url.format(faceListId),headers=headers,params=params_add_face,json={\"url\":img_url})\n",
    "    return r_add_face.status_code#返回出状态码"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "200"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "AddFace(\"http://huangjieqi.gitee.io/picture_storage/Autumnhui.jpg\",\"丘天惠\")\n",
    "AddFace(\"http://huangjieqi.gitee.io/picture_storage/L-Tony-info.jpg\",\"林嘉茵\")\n",
    "AddFace(\"http://huangjieqi.gitee.io/picture_storage/TLINGP.jpg\",\"汤玲萍\")\n",
    "AddFace(\"http://huangjieqi.gitee.io/picture_storage/WenYanZeng.jpg\",\"曾雯燕\")\n",
    "AddFace(\"http://huangjieqi.gitee.io/picture_storage/XIEIC.jpg\",\"谢依希\")\n",
    "AddFace(\"http://huangjieqi.gitee.io/picture_storage/YuecongYang.png\",\"杨悦聪\")\n",
    "AddFace(\"http://huangjieqi.gitee.io/picture_storage/Zoezhouyu.jpg\",\"周雨\")\n",
    "AddFace(\"http://huangjieqi.gitee.io/picture_storage/crayon-heimi.jpg\",\"刘瑜鹏\")\n",
    "AddFace(\"http://huangjieqi.gitee.io/picture_storage/jiayichen.jpg\",\"陈嘉仪\")\n",
    "AddFace(\"http://huangjieqi.gitee.io/picture_storage/kg2000.jpg\",\"徐旖芊\")\n",
    "AddFace(\"http://huangjieqi.gitee.io/picture_storage/liuxinrujiayou.jpg\",\"刘心如\")\n",
    "AddFace(\"http://huangjieqi.gitee.io/picture_storage/liuyu19.png\",\"刘宇\")\n",
    "AddFace(\"http://huangjieqi.gitee.io/picture_storage/ltco.jpg\",\"李婷\")\n",
    "AddFace(\"http://huangjieqi.gitee.io/picture_storage/lucaszy.jpg\",\"黄智毅\")\n",
    "AddFace(\"http://huangjieqi.gitee.io/picture_storage/pingzi0211.jpg\",\"黄慧文\")\n",
    "AddFace(\"http://huangjieqi.gitee.io/picture_storage/shmimy-cn.jpg\",\"张铭睿\")\n",
    "AddFace(\"http://huangjieqi.gitee.io/picture_storage/yichenting.jpg\",\"陈婷\")\n",
    "AddFace(\"http://huangjieqi.gitee.io/picture_storage/coco022.jpg\",\"洪可凡\")\n",
    "AddFace(\"http://huangjieqi.gitee.io/picture_storage/lujizhi.png\",\"卢继志\")\n",
    "AddFace(\"http://huangjieqi.gitee.io/picture_storage/zzlhyy.jpg\",\"张梓乐\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'persistedFaces': [{'persistedFaceId': '0b0504ce-6453-4ba4-889e-1efbae8b8212',\n",
       "   'userData': 'lady'},\n",
       "  {'persistedFaceId': '522e5060-918b-45a3-8c47-05f709ea67a3',\n",
       "   'userData': '丘天惠'},\n",
       "  {'persistedFaceId': '9b05d1e9-855e-4de5-b2c8-8329839772bb',\n",
       "   'userData': '林嘉茵'},\n",
       "  {'persistedFaceId': '34827907-8a50-4c89-b62e-83b918d4cfad',\n",
       "   'userData': '汤玲萍'},\n",
       "  {'persistedFaceId': 'fa826e3c-a9f5-43d9-a62b-921bb2e07c8d',\n",
       "   'userData': '曾雯燕'},\n",
       "  {'persistedFaceId': 'd98ea18b-6035-47c7-955d-a38024036241',\n",
       "   'userData': '谢依希'},\n",
       "  {'persistedFaceId': '215ed052-7cf5-4bfe-bfd0-769f2c8e1832',\n",
       "   'userData': '杨悦聪'},\n",
       "  {'persistedFaceId': '3f3201ab-aefe-4d9f-8c8c-9a79580ad909',\n",
       "   'userData': '周雨'},\n",
       "  {'persistedFaceId': '242b43ff-0e06-45ae-a20f-95ccf1b4fa57',\n",
       "   'userData': '刘瑜鹏'},\n",
       "  {'persistedFaceId': 'aaa13903-e309-4a87-8205-de694ba8d60b',\n",
       "   'userData': '陈嘉仪'},\n",
       "  {'persistedFaceId': '050dfbd6-f433-4144-aa32-1a63d47dd231',\n",
       "   'userData': '徐旖芊'},\n",
       "  {'persistedFaceId': '68e03186-5a9f-4f5a-b64c-3d128e1f51b0',\n",
       "   'userData': '刘心如'},\n",
       "  {'persistedFaceId': 'c059beb1-7300-43f1-862b-6a4389ada3dd',\n",
       "   'userData': '刘宇'},\n",
       "  {'persistedFaceId': '0470a094-4bf9-4daa-98fd-ed1859874083',\n",
       "   'userData': '李婷'},\n",
       "  {'persistedFaceId': '52151691-6433-47f1-a493-fb1ee4a8c603',\n",
       "   'userData': '黄智毅'},\n",
       "  {'persistedFaceId': 'f411e7a8-65a9-4ca8-8dbd-0059473f2224',\n",
       "   'userData': '黄慧文'},\n",
       "  {'persistedFaceId': '8234bb62-f2a1-46c0-9593-542a136398c1',\n",
       "   'userData': '张铭睿'},\n",
       "  {'persistedFaceId': 'e9e2f03d-c57a-49ed-b85c-060056f42b37',\n",
       "   'userData': '陈婷'}],\n",
       " 'faceListId': 'dengyingxin02',\n",
       " 'name': 'sample_list',\n",
       " 'userData': '相册'}"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# Get facelist\n",
    "get_facelist_url = \"https://apidyx.cognitiveservices.azure.com/face/v1.0/facelists/{}\"#学生填写\n",
    "r_get_facelist = requests.get(get_facelist_url.format(faceListId),headers=headers)#学生填写\n",
    "r_get_facelist.json()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [],
   "source": [
    "faceListId =\"dengyingxin02\" #删除人脸列表 # 学生填写"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Detect face 删除列表内人脸id\n",
    "delete_face_url =\"https://apidyx.cognitiveservices.azure.com/face/v1.0/facelists/{}/persistedfaces/{}\"#学生填写\n",
    "\n",
    "assert subscription_key\n",
    "persistedFaceId = r_add_face.json()[\"persistedFaceId\"]\n",
    "# 直接取上面获得的ID{'persistedFaceId': '85019b1c0337446294c317daeb3f435d'} \n",
    "\n",
    "headers = {\n",
    "    # Request headers\n",
    "    'Content-Type': 'application/json',\n",
    "    'Ocp-Apim-Subscription-Key': subscription_key,\n",
    "}\n",
    "\n",
    "# 注意requests请求为delete\n",
    "r_delete_face = requests.delete(delete_face_url.format(faceListId,persistedFaceId),headers=headers)#学生填写"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<Response [200]>"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r_delete_face"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Detect face 删除人脸列表\n",
    "delete_facelist_url =\"https://apidyx.cognitiveservices.azure.com/face/v1.0/facelists/{}\"#学生填写\n",
    "assert subscription_key\n",
    "\n",
    "headers = {\n",
    "    # Request headers\n",
    "    'Content-Type': 'application/json',\n",
    "    'Ocp-Apim-Subscription-Key': subscription_key,\n",
    "}\n",
    "\n",
    "# format内直接删除\n",
    "# 注意requests请求为delete\n",
    "r_delete_facelist = requests.delete(delete_facelist_url.format(faceListId),headers=headers)#学生填写"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<Response [200]>"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r_delete_facelist"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[{'faceId': 'c5e4176b-e1fd-4b0d-a14b-86a3a8e050b2',\n",
       "  'faceRectangle': {'top': 341, 'left': 1083, 'width': 233, 'height': 233},\n",
       "  'faceAttributes': {'smile': 0.995,\n",
       "   'headPose': {'pitch': -5.3, 'roll': -4.1, 'yaw': -14.2},\n",
       "   'gender': 'male',\n",
       "   'age': 21.0,\n",
       "   'facialHair': {'moustache': 0.1, 'beard': 0.1, 'sideburns': 0.1},\n",
       "   'glasses': 'NoGlasses',\n",
       "   'emotion': {'anger': 0.0,\n",
       "    'contempt': 0.0,\n",
       "    'disgust': 0.0,\n",
       "    'fear': 0.0,\n",
       "    'happiness': 0.995,\n",
       "    'neutral': 0.005,\n",
       "    'sadness': 0.0,\n",
       "    'surprise': 0.0},\n",
       "   'blur': {'blurLevel': 'low', 'value': 0.0},\n",
       "   'exposure': {'exposureLevel': 'goodExposure', 'value': 0.54},\n",
       "   'noise': {'noiseLevel': 'low', 'value': 0.08},\n",
       "   'makeup': {'eyeMakeup': True, 'lipMakeup': True},\n",
       "   'accessories': [],\n",
       "   'occlusion': {'foreheadOccluded': False,\n",
       "    'eyeOccluded': False,\n",
       "    'mouthOccluded': False},\n",
       "   'hair': {'bald': 0.03,\n",
       "    'invisible': False,\n",
       "    'hairColor': [{'color': 'brown', 'confidence': 0.99},\n",
       "     {'color': 'blond', 'confidence': 0.85},\n",
       "     {'color': 'gray', 'confidence': 0.65},\n",
       "     {'color': 'red', 'confidence': 0.18},\n",
       "     {'color': 'black', 'confidence': 0.08},\n",
       "     {'color': 'other', 'confidence': 0.05},\n",
       "     {'color': 'white', 'confidence': 0.0}]}}},\n",
       " {'faceId': 'c22e95b3-5956-4e5f-82f0-e0ae74279ba9',\n",
       "  'faceRectangle': {'top': 219, 'left': 247, 'width': 217, 'height': 217},\n",
       "  'faceAttributes': {'smile': 0.005,\n",
       "   'headPose': {'pitch': -14.3, 'roll': -7.8, 'yaw': -22.2},\n",
       "   'gender': 'male',\n",
       "   'age': 22.0,\n",
       "   'facialHair': {'moustache': 0.1, 'beard': 0.1, 'sideburns': 0.1},\n",
       "   'glasses': 'NoGlasses',\n",
       "   'emotion': {'anger': 0.0,\n",
       "    'contempt': 0.005,\n",
       "    'disgust': 0.0,\n",
       "    'fear': 0.0,\n",
       "    'happiness': 0.005,\n",
       "    'neutral': 0.986,\n",
       "    'sadness': 0.001,\n",
       "    'surprise': 0.002},\n",
       "   'blur': {'blurLevel': 'low', 'value': 0.21},\n",
       "   'exposure': {'exposureLevel': 'goodExposure', 'value': 0.63},\n",
       "   'noise': {'noiseLevel': 'low', 'value': 0.11},\n",
       "   'makeup': {'eyeMakeup': True, 'lipMakeup': True},\n",
       "   'accessories': [],\n",
       "   'occlusion': {'foreheadOccluded': False,\n",
       "    'eyeOccluded': False,\n",
       "    'mouthOccluded': False},\n",
       "   'hair': {'bald': 0.03,\n",
       "    'invisible': False,\n",
       "    'hairColor': [{'color': 'brown', 'confidence': 0.95},\n",
       "     {'color': 'blond', 'confidence': 0.6},\n",
       "     {'color': 'black', 'confidence': 0.55},\n",
       "     {'color': 'gray', 'confidence': 0.46},\n",
       "     {'color': 'other', 'confidence': 0.15},\n",
       "     {'color': 'red', 'confidence': 0.15},\n",
       "     {'color': 'white', 'confidence': 0.0}]}}}]"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 3、检测人脸的id\n",
    "# replace <My Endpoint String> with the string from your endpoint URL\n",
    "face_api_url = 'https://apidyx.cognitiveservices.azure.com/face/v1.0/detect'\n",
    "\n",
    "# 请求正文\n",
    "image_url = 'https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1603473599289&di=dd9a3ff8e9c8e9526f8b7535b419a072&imgtype=0&src=http%3A%2F%2Fb-ssl.duitang.com%2Fuploads%2Fitem%2F201810%2F31%2F20181031164830_u4NY2.jpeg'\n",
    "\n",
    "headers = {'Ocp-Apim-Subscription-Key': subscription_key}\n",
    "\n",
    "# 请求参数\n",
    "params = {\n",
    "    'returnFaceId': 'true',\n",
    "    'returnFaceLandmarks': 'false',\n",
    "    # 选择model\n",
    "    'recognitionModel':'recognition_03',#此参数需与facelist参数一致\n",
    "    'detectionModel':'detection_01',\n",
    "    # 可选参数,请仔细阅读API文档\n",
    "    'returnFaceAttributes': 'age,gender,headPose,smile,facialHair,glasses,emotion,hair,makeup,occlusion,accessories,blur,exposure,noise',\n",
    "}\n",
    "\n",
    "response = requests.post(face_api_url, params=params,headers=headers, json={\"url\": image_url})\n",
    "# json.dumps 将json--->字符串\n",
    "response.json()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 48,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "b''"
      ]
     },
     "execution_count": 48,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 4.返回人脸相似置信度\n",
    "import requests\n",
    "# 1.创建人脸列表\n",
    "faceListId = \"dengyingxin04\" # 设置人脸列表ID\n",
    "create_facelists_url =\"https://apidyx.cognitiveservices.azure.com/face/v1.0/facelists/{}\" # URL（见API文档）\n",
    "subscription_key =\"d4147415eb404d1fb95b61fdb32649ac\" # 密钥\n",
    "# 请求头\n",
    "headers = {\n",
    "    'Content-Type': 'application/json',\n",
    "    'Ocp-Apim-Subscription-Key': subscription_key,\n",
    "}\n",
    "# 相关信息及识别模型的选用\n",
    "data = {\n",
    "  \"name\": \"sample_list\",\n",
    "  \"userData\": \"相册\",\n",
    "  \"recognitionModel\": \"recognition_03\"\n",
    "}\n",
    "# 发送请求\n",
    "r_create = requests.put(create_facelists_url.format(faceListId), headers=headers, json=data)\n",
    "r_create.content"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 49,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "200"
      ]
     },
     "execution_count": 49,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "dd_face_url =\"https://apidyx.cognitiveservices.azure.com/face/v1.0/facelists/{}/persistedfaces\"\n",
    "assert subscription_key\n",
    "headers = {\n",
    "    'Content-Type': 'application/json',\n",
    "    'Ocp-Apim-Subscription-Key': subscription_key,\n",
    "}\n",
    "# 人脸相片地址\n",
    "img_url =\"https://www.sonystyle.com.cn/content/dam/sonystyle/products/ilc/e-body/ilce-7c/feature/ilce_7c_01_200915.jpg\"\n",
    "# 人脸数据\n",
    "params_add_face={\n",
    "    \"userData\":\"lady\"\n",
    "}\n",
    "# 发送请求\n",
    "r_add_face = requests.post(add_face_url.format(faceListId),headers=headers,params=params_add_face,json={\"url\":img_url})\n",
    "r_add_face.status_code"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 56,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'persistedFaceId': 'c7f7ea38-23f5-4b53-ae0c-e822729abb4d'}"
      ]
     },
     "execution_count": 56,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r_add_face.json() # 返回persistedFaceId"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 57,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 封装成函数方便添加图片\n",
    "def AddFace(img_url=str,userData=str):\n",
    "    add_face_url =\"https://apidyx.cognitiveservices.azure.com/face/v1.0/facelists/{}/persistedFaces\"\n",
    "    assert subscription_key\n",
    "    headers = {\n",
    "        # Request headers\n",
    "        'Content-Type': 'application/json',\n",
    "        'Ocp-Apim-Subscription-Key': subscription_key,\n",
    "    }\n",
    "    img_url = img_url\n",
    "\n",
    "    params_add_face={\n",
    "        \"userData\":userData\n",
    "    }\n",
    "    r_add_face = requests.post(add_face_url.format(faceListId),headers=headers,params=params_add_face,json={\"url\":img_url})\n",
    "    return r_add_face.status_code#返回出状态码"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 58,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "200"
      ]
     },
     "execution_count": 58,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "AddFace(\"http://huangjieqi.gitee.io/picture_storage/Autumnhui.jpg\",\"丘天惠\")\n",
    "AddFace(\"http://huangjieqi.gitee.io/picture_storage/L-Tony-info.jpg\",\"林嘉茵\")\n",
    "AddFace(\"http://huangjieqi.gitee.io/picture_storage/TLINGP.jpg\",\"汤玲萍\")\n",
    "AddFace(\"http://huangjieqi.gitee.io/picture_storage/WenYanZeng.jpg\",\"曾雯燕\")\n",
    "AddFace(\"http://huangjieqi.gitee.io/picture_storage/XIEIC.jpg\",\"谢依希\")\n",
    "AddFace(\"http://huangjieqi.gitee.io/picture_storage/YuecongYang.png\",\"杨悦聪\")\n",
    "AddFace(\"http://huangjieqi.gitee.io/picture_storage/Zoezhouyu.jpg\",\"周雨\")\n",
    "AddFace(\"http://huangjieqi.gitee.io/picture_storage/crayon-heimi.jpg\",\"刘瑜鹏\")\n",
    "AddFace(\"http://huangjieqi.gitee.io/picture_storage/jiayichen.jpg\",\"陈嘉仪\")\n",
    "AddFace(\"http://huangjieqi.gitee.io/picture_storage/kg2000.jpg\",\"徐旖芊\")\n",
    "AddFace(\"http://huangjieqi.gitee.io/picture_storage/liuxinrujiayou.jpg\",\"刘心如\")\n",
    "AddFace(\"http://huangjieqi.gitee.io/picture_storage/liuyu19.png\",\"刘宇\")\n",
    "AddFace(\"http://huangjieqi.gitee.io/picture_storage/ltco.jpg\",\"李婷\")\n",
    "AddFace(\"http://huangjieqi.gitee.io/picture_storage/lucaszy.jpg\",\"黄智毅\")\n",
    "AddFace(\"http://huangjieqi.gitee.io/picture_storage/pingzi0211.jpg\",\"黄慧文\")\n",
    "AddFace(\"http://huangjieqi.gitee.io/picture_storage/shmimy-cn.jpg\",\"张铭睿\")\n",
    "AddFace(\"http://huangjieqi.gitee.io/picture_storage/yichenting.jpg\",\"陈婷\")\n",
    "AddFace(\"http://huangjieqi.gitee.io/picture_storage/coco022.jpg\",\"洪可凡\")\n",
    "AddFace(\"http://huangjieqi.gitee.io/picture_storage/lujizhi.png\",\"卢继志\")\n",
    "AddFace(\"http://huangjieqi.gitee.io/picture_storage/zzlhyy.jpg\",\"张梓乐\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 59,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'persistedFaces': [{'persistedFaceId': 'c7f7ea38-23f5-4b53-ae0c-e822729abb4d',\n",
       "   'userData': 'lady'},\n",
       "  {'persistedFaceId': '2a866d17-58a4-4c69-a189-66a6d04a2a4c',\n",
       "   'userData': '丘天惠'},\n",
       "  {'persistedFaceId': '6c0ed8f2-8aa6-4184-b2e3-54e773434104',\n",
       "   'userData': '林嘉茵'},\n",
       "  {'persistedFaceId': '0dcb1154-a5a6-41c6-a4cf-fb280f338184',\n",
       "   'userData': '汤玲萍'},\n",
       "  {'persistedFaceId': '76bf9cb2-0a21-4346-857e-646b722d184f',\n",
       "   'userData': '曾雯燕'},\n",
       "  {'persistedFaceId': '47907d3d-dbe6-4909-931a-384d96e52777',\n",
       "   'userData': '谢依希'},\n",
       "  {'persistedFaceId': '35f64572-365c-4780-99fd-a36cfae6a4da',\n",
       "   'userData': '杨悦聪'},\n",
       "  {'persistedFaceId': '333d71ad-e072-431c-9765-878fcd93a960',\n",
       "   'userData': '周雨'},\n",
       "  {'persistedFaceId': 'a620dc5e-e781-4ceb-9e35-bf6f17e1b720',\n",
       "   'userData': '刘瑜鹏'},\n",
       "  {'persistedFaceId': 'ee5505b3-ad39-48bf-a291-2f4f7b310690',\n",
       "   'userData': '陈嘉仪'},\n",
       "  {'persistedFaceId': 'e4a8f43c-8b36-4305-98bd-fc3286eceb91',\n",
       "   'userData': '徐旖芊'},\n",
       "  {'persistedFaceId': 'd196ac80-99d1-4f61-9c4a-aa3d0cacab32',\n",
       "   'userData': '刘心如'},\n",
       "  {'persistedFaceId': '1c83c485-324a-4e8a-85fe-c6fd08707fe9',\n",
       "   'userData': '刘宇'},\n",
       "  {'persistedFaceId': '19778ad0-47ff-4197-8ad9-19e8678c2b2e',\n",
       "   'userData': '李婷'},\n",
       "  {'persistedFaceId': '51ea03e0-006c-4262-ad96-24fc9517fe60',\n",
       "   'userData': '黄智毅'},\n",
       "  {'persistedFaceId': '0ceaf5bd-5d5a-4f6e-88c4-608c010b905b',\n",
       "   'userData': '黄慧文'},\n",
       "  {'persistedFaceId': '3bcd07f8-7c91-4dfa-a62f-84713a678716',\n",
       "   'userData': '张铭睿'},\n",
       "  {'persistedFaceId': '04e9f6d5-5f7f-4be0-964e-a5a9ac680280',\n",
       "   'userData': '陈婷'},\n",
       "  {'persistedFaceId': '74d40b11-d320-40ba-8f59-e4f57dc4c4c7',\n",
       "   'userData': '洪可凡'},\n",
       "  {'persistedFaceId': '018c40a4-7f24-449b-a7af-9ff44b176440',\n",
       "   'userData': '卢继志'},\n",
       "  {'persistedFaceId': '5395210a-05e0-46c7-84e0-529aeb981428',\n",
       "   'userData': '张梓乐'}],\n",
       " 'faceListId': 'dengyingxin04',\n",
       " 'name': 'sample_list',\n",
       " 'userData': '相册'}"
      ]
     },
     "execution_count": 59,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# Get facelist\n",
    "get_facelist_url = \"https://apidyx.cognitiveservices.azure.com/face/v1.0/facelists/{}\"#学生填写\n",
    "r_get_facelist = requests.get(get_facelist_url.format(faceListId),headers=headers)#学生填写\n",
    "r_get_facelist.json()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 60,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[{'persistedFaceId': 'e4a8f43c-8b36-4305-98bd-fc3286eceb91',\n",
       "  'confidence': 0.22356},\n",
       " {'persistedFaceId': 'ee5505b3-ad39-48bf-a291-2f4f7b310690',\n",
       "  'confidence': 0.183},\n",
       " {'persistedFaceId': 'd196ac80-99d1-4f61-9c4a-aa3d0cacab32',\n",
       "  'confidence': 0.12203},\n",
       " {'persistedFaceId': '35f64572-365c-4780-99fd-a36cfae6a4da',\n",
       "  'confidence': 0.10102},\n",
       " {'persistedFaceId': '04e9f6d5-5f7f-4be0-964e-a5a9ac680280',\n",
       "  'confidence': 0.09832},\n",
       " {'persistedFaceId': 'c7f7ea38-23f5-4b53-ae0c-e822729abb4d',\n",
       "  'confidence': 0.09758},\n",
       " {'persistedFaceId': 'a620dc5e-e781-4ceb-9e35-bf6f17e1b720',\n",
       "  'confidence': 0.09664},\n",
       " {'persistedFaceId': '5395210a-05e0-46c7-84e0-529aeb981428',\n",
       "  'confidence': 0.09424},\n",
       " {'persistedFaceId': '0dcb1154-a5a6-41c6-a4cf-fb280f338184',\n",
       "  'confidence': 0.0941},\n",
       " {'persistedFaceId': '19778ad0-47ff-4197-8ad9-19e8678c2b2e',\n",
       "  'confidence': 0.09389}]"
      ]
     },
     "execution_count": 60,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "findsimilars_url = \"https://apidyx.cognitiveservices.azure.com/face/v1.0/findsimilars\"\n",
    "# 请求正文 faceID需要先检测一张照片获取\n",
    "data_findsimilars = {\n",
    "    \"faceId\":\"c5e4176b-e1fd-4b0d-a14b-86a3a8e050b2\", #取上方的faceID\n",
    "    \"faceListId\": \"dengyingxin04\",\n",
    "    \"maxNumOfCandidatesReturned\": 10,\n",
    "    \"mode\": \"matchFace\" #matchPerson #一种为验证模式，一种为相似值模式\n",
    "    }\n",
    "r_findsimilars = requests.post(findsimilars_url,headers=headers,json=data_findsimilars)\n",
    "r_findsimilars.json()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 62,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'persistedFaces': [{'persistedFaceId': 'c7f7ea38-23f5-4b53-ae0c-e822729abb4d',\n",
       "   'userData': 'lady'},\n",
       "  {'persistedFaceId': '2a866d17-58a4-4c69-a189-66a6d04a2a4c',\n",
       "   'userData': '丘天惠'},\n",
       "  {'persistedFaceId': '6c0ed8f2-8aa6-4184-b2e3-54e773434104',\n",
       "   'userData': '林嘉茵'},\n",
       "  {'persistedFaceId': '0dcb1154-a5a6-41c6-a4cf-fb280f338184',\n",
       "   'userData': '汤玲萍'},\n",
       "  {'persistedFaceId': '76bf9cb2-0a21-4346-857e-646b722d184f',\n",
       "   'userData': '曾雯燕'},\n",
       "  {'persistedFaceId': '47907d3d-dbe6-4909-931a-384d96e52777',\n",
       "   'userData': '谢依希'},\n",
       "  {'persistedFaceId': '35f64572-365c-4780-99fd-a36cfae6a4da',\n",
       "   'userData': '杨悦聪'},\n",
       "  {'persistedFaceId': '333d71ad-e072-431c-9765-878fcd93a960',\n",
       "   'userData': '周雨'},\n",
       "  {'persistedFaceId': 'a620dc5e-e781-4ceb-9e35-bf6f17e1b720',\n",
       "   'userData': '刘瑜鹏'},\n",
       "  {'persistedFaceId': 'ee5505b3-ad39-48bf-a291-2f4f7b310690',\n",
       "   'userData': '陈嘉仪'},\n",
       "  {'persistedFaceId': 'e4a8f43c-8b36-4305-98bd-fc3286eceb91',\n",
       "   'userData': '徐旖芊'},\n",
       "  {'persistedFaceId': 'd196ac80-99d1-4f61-9c4a-aa3d0cacab32',\n",
       "   'userData': '刘心如'},\n",
       "  {'persistedFaceId': '1c83c485-324a-4e8a-85fe-c6fd08707fe9',\n",
       "   'userData': '刘宇'},\n",
       "  {'persistedFaceId': '19778ad0-47ff-4197-8ad9-19e8678c2b2e',\n",
       "   'userData': '李婷'},\n",
       "  {'persistedFaceId': '51ea03e0-006c-4262-ad96-24fc9517fe60',\n",
       "   'userData': '黄智毅'},\n",
       "  {'persistedFaceId': '0ceaf5bd-5d5a-4f6e-88c4-608c010b905b',\n",
       "   'userData': '黄慧文'},\n",
       "  {'persistedFaceId': '3bcd07f8-7c91-4dfa-a62f-84713a678716',\n",
       "   'userData': '张铭睿'},\n",
       "  {'persistedFaceId': '04e9f6d5-5f7f-4be0-964e-a5a9ac680280',\n",
       "   'userData': '陈婷'},\n",
       "  {'persistedFaceId': '74d40b11-d320-40ba-8f59-e4f57dc4c4c7',\n",
       "   'userData': '洪可凡'},\n",
       "  {'persistedFaceId': '018c40a4-7f24-449b-a7af-9ff44b176440',\n",
       "   'userData': '卢继志'},\n",
       "  {'persistedFaceId': '5395210a-05e0-46c7-84e0-529aeb981428',\n",
       "   'userData': '张梓乐'}],\n",
       " 'faceListId': 'dengyingxin04',\n",
       " 'name': 'sample_list',\n",
       " 'userData': '相册'}"
      ]
     },
     "execution_count": 62,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 查看列表\n",
    "get_facelist_url = \"https://apidyx.cognitiveservices.azure.com/face/v1.0/facelists/{}\"\n",
    "r_get_facelist = requests.get(get_facelist_url.format(faceListId),headers=headers)\n",
    "r_get_facelist.json()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 63,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>persistedFaceId</th>\n",
       "      <th>userData</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>c7f7ea38-23f5-4b53-ae0c-e822729abb4d</td>\n",
       "      <td>lady</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>2a866d17-58a4-4c69-a189-66a6d04a2a4c</td>\n",
       "      <td>丘天惠</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>6c0ed8f2-8aa6-4184-b2e3-54e773434104</td>\n",
       "      <td>林嘉茵</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>0dcb1154-a5a6-41c6-a4cf-fb280f338184</td>\n",
       "      <td>汤玲萍</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>76bf9cb2-0a21-4346-857e-646b722d184f</td>\n",
       "      <td>曾雯燕</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>47907d3d-dbe6-4909-931a-384d96e52777</td>\n",
       "      <td>谢依希</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>35f64572-365c-4780-99fd-a36cfae6a4da</td>\n",
       "      <td>杨悦聪</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>333d71ad-e072-431c-9765-878fcd93a960</td>\n",
       "      <td>周雨</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8</th>\n",
       "      <td>a620dc5e-e781-4ceb-9e35-bf6f17e1b720</td>\n",
       "      <td>刘瑜鹏</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9</th>\n",
       "      <td>ee5505b3-ad39-48bf-a291-2f4f7b310690</td>\n",
       "      <td>陈嘉仪</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>10</th>\n",
       "      <td>e4a8f43c-8b36-4305-98bd-fc3286eceb91</td>\n",
       "      <td>徐旖芊</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>11</th>\n",
       "      <td>d196ac80-99d1-4f61-9c4a-aa3d0cacab32</td>\n",
       "      <td>刘心如</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>12</th>\n",
       "      <td>1c83c485-324a-4e8a-85fe-c6fd08707fe9</td>\n",
       "      <td>刘宇</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>13</th>\n",
       "      <td>19778ad0-47ff-4197-8ad9-19e8678c2b2e</td>\n",
       "      <td>李婷</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>14</th>\n",
       "      <td>51ea03e0-006c-4262-ad96-24fc9517fe60</td>\n",
       "      <td>黄智毅</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>15</th>\n",
       "      <td>0ceaf5bd-5d5a-4f6e-88c4-608c010b905b</td>\n",
       "      <td>黄慧文</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>16</th>\n",
       "      <td>3bcd07f8-7c91-4dfa-a62f-84713a678716</td>\n",
       "      <td>张铭睿</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>17</th>\n",
       "      <td>04e9f6d5-5f7f-4be0-964e-a5a9ac680280</td>\n",
       "      <td>陈婷</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>18</th>\n",
       "      <td>74d40b11-d320-40ba-8f59-e4f57dc4c4c7</td>\n",
       "      <td>洪可凡</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>19</th>\n",
       "      <td>018c40a4-7f24-449b-a7af-9ff44b176440</td>\n",
       "      <td>卢继志</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>20</th>\n",
       "      <td>5395210a-05e0-46c7-84e0-529aeb981428</td>\n",
       "      <td>张梓乐</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                         persistedFaceId userData\n",
       "0   c7f7ea38-23f5-4b53-ae0c-e822729abb4d     lady\n",
       "1   2a866d17-58a4-4c69-a189-66a6d04a2a4c      丘天惠\n",
       "2   6c0ed8f2-8aa6-4184-b2e3-54e773434104      林嘉茵\n",
       "3   0dcb1154-a5a6-41c6-a4cf-fb280f338184      汤玲萍\n",
       "4   76bf9cb2-0a21-4346-857e-646b722d184f      曾雯燕\n",
       "5   47907d3d-dbe6-4909-931a-384d96e52777      谢依希\n",
       "6   35f64572-365c-4780-99fd-a36cfae6a4da      杨悦聪\n",
       "7   333d71ad-e072-431c-9765-878fcd93a960       周雨\n",
       "8   a620dc5e-e781-4ceb-9e35-bf6f17e1b720      刘瑜鹏\n",
       "9   ee5505b3-ad39-48bf-a291-2f4f7b310690      陈嘉仪\n",
       "10  e4a8f43c-8b36-4305-98bd-fc3286eceb91      徐旖芊\n",
       "11  d196ac80-99d1-4f61-9c4a-aa3d0cacab32      刘心如\n",
       "12  1c83c485-324a-4e8a-85fe-c6fd08707fe9       刘宇\n",
       "13  19778ad0-47ff-4197-8ad9-19e8678c2b2e       李婷\n",
       "14  51ea03e0-006c-4262-ad96-24fc9517fe60      黄智毅\n",
       "15  0ceaf5bd-5d5a-4f6e-88c4-608c010b905b      黄慧文\n",
       "16  3bcd07f8-7c91-4dfa-a62f-84713a678716      张铭睿\n",
       "17  04e9f6d5-5f7f-4be0-964e-a5a9ac680280       陈婷\n",
       "18  74d40b11-d320-40ba-8f59-e4f57dc4c4c7      洪可凡\n",
       "19  018c40a4-7f24-449b-a7af-9ff44b176440      卢继志\n",
       "20  5395210a-05e0-46c7-84e0-529aeb981428      张梓乐"
      ]
     },
     "execution_count": 63,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import pandas as pd\n",
    "#facelist里面的数据\n",
    "adf = pd.json_normalize(r_get_facelist.json()[\"persistedFaces\"])# 升级pandas才能运行\n",
    "adf"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 65,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>persistedFaceId</th>\n",
       "      <th>confidence</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>e4a8f43c-8b36-4305-98bd-fc3286eceb91</td>\n",
       "      <td>0.22356</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>ee5505b3-ad39-48bf-a291-2f4f7b310690</td>\n",
       "      <td>0.18300</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>d196ac80-99d1-4f61-9c4a-aa3d0cacab32</td>\n",
       "      <td>0.12203</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>35f64572-365c-4780-99fd-a36cfae6a4da</td>\n",
       "      <td>0.10102</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>04e9f6d5-5f7f-4be0-964e-a5a9ac680280</td>\n",
       "      <td>0.09832</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>c7f7ea38-23f5-4b53-ae0c-e822729abb4d</td>\n",
       "      <td>0.09758</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>a620dc5e-e781-4ceb-9e35-bf6f17e1b720</td>\n",
       "      <td>0.09664</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>5395210a-05e0-46c7-84e0-529aeb981428</td>\n",
       "      <td>0.09424</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8</th>\n",
       "      <td>0dcb1154-a5a6-41c6-a4cf-fb280f338184</td>\n",
       "      <td>0.09410</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9</th>\n",
       "      <td>19778ad0-47ff-4197-8ad9-19e8678c2b2e</td>\n",
       "      <td>0.09389</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                        persistedFaceId  confidence\n",
       "0  e4a8f43c-8b36-4305-98bd-fc3286eceb91     0.22356\n",
       "1  ee5505b3-ad39-48bf-a291-2f4f7b310690     0.18300\n",
       "2  d196ac80-99d1-4f61-9c4a-aa3d0cacab32     0.12203\n",
       "3  35f64572-365c-4780-99fd-a36cfae6a4da     0.10102\n",
       "4  04e9f6d5-5f7f-4be0-964e-a5a9ac680280     0.09832\n",
       "5  c7f7ea38-23f5-4b53-ae0c-e822729abb4d     0.09758\n",
       "6  a620dc5e-e781-4ceb-9e35-bf6f17e1b720     0.09664\n",
       "7  5395210a-05e0-46c7-84e0-529aeb981428     0.09424\n",
       "8  0dcb1154-a5a6-41c6-a4cf-fb280f338184     0.09410\n",
       "9  19778ad0-47ff-4197-8ad9-19e8678c2b2e     0.09389"
      ]
     },
     "execution_count": 65,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 返回相似度的数据\n",
    "find_df = pd.json_normalize(r_findsimilars.json())# 升级pandas才能运行\n",
    "find_df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 68,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>persistedFaceId</th>\n",
       "      <th>userData</th>\n",
       "      <th>confidence</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>e4a8f43c-8b36-4305-98bd-fc3286eceb91</td>\n",
       "      <td>徐旖芊</td>\n",
       "      <td>0.22356</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>ee5505b3-ad39-48bf-a291-2f4f7b310690</td>\n",
       "      <td>陈嘉仪</td>\n",
       "      <td>0.18300</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>d196ac80-99d1-4f61-9c4a-aa3d0cacab32</td>\n",
       "      <td>刘心如</td>\n",
       "      <td>0.12203</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>35f64572-365c-4780-99fd-a36cfae6a4da</td>\n",
       "      <td>杨悦聪</td>\n",
       "      <td>0.10102</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8</th>\n",
       "      <td>04e9f6d5-5f7f-4be0-964e-a5a9ac680280</td>\n",
       "      <td>陈婷</td>\n",
       "      <td>0.09832</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>c7f7ea38-23f5-4b53-ae0c-e822729abb4d</td>\n",
       "      <td>lady</td>\n",
       "      <td>0.09758</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>a620dc5e-e781-4ceb-9e35-bf6f17e1b720</td>\n",
       "      <td>刘瑜鹏</td>\n",
       "      <td>0.09664</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9</th>\n",
       "      <td>5395210a-05e0-46c7-84e0-529aeb981428</td>\n",
       "      <td>张梓乐</td>\n",
       "      <td>0.09424</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>0dcb1154-a5a6-41c6-a4cf-fb280f338184</td>\n",
       "      <td>汤玲萍</td>\n",
       "      <td>0.09410</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>19778ad0-47ff-4197-8ad9-19e8678c2b2e</td>\n",
       "      <td>李婷</td>\n",
       "      <td>0.09389</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                        persistedFaceId userData  confidence\n",
       "5  e4a8f43c-8b36-4305-98bd-fc3286eceb91      徐旖芊     0.22356\n",
       "4  ee5505b3-ad39-48bf-a291-2f4f7b310690      陈嘉仪     0.18300\n",
       "6  d196ac80-99d1-4f61-9c4a-aa3d0cacab32      刘心如     0.12203\n",
       "2  35f64572-365c-4780-99fd-a36cfae6a4da      杨悦聪     0.10102\n",
       "8  04e9f6d5-5f7f-4be0-964e-a5a9ac680280       陈婷     0.09832\n",
       "0  c7f7ea38-23f5-4b53-ae0c-e822729abb4d     lady     0.09758\n",
       "3  a620dc5e-e781-4ceb-9e35-bf6f17e1b720      刘瑜鹏     0.09664\n",
       "9  5395210a-05e0-46c7-84e0-529aeb981428      张梓乐     0.09424\n",
       "1  0dcb1154-a5a6-41c6-a4cf-fb280f338184      汤玲萍     0.09410\n",
       "7  19778ad0-47ff-4197-8ad9-19e8678c2b2e       李婷     0.09389"
      ]
     },
     "execution_count": 68,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#合并\n",
    "pd.merge(adf, find_df,how='inner', on='persistedFaceId').sort_values(by=\"confidence\",ascending = False)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.3"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
